<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $msg                  = 'Record Not Found...!';
  $companyName          = "";
  $challanType          = "";
  $deliveryChallanArray = array();
  $deliveryDetailArray  = array();
  $deliveryChallanCount = 0;
  
	/////////////////  delivery date :Start
	if(isset($_REQUEST['fromDateMonth']))
		$fromDate  = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-01";
	else
		$fromDate  = date("Y-m-d");
	
	if(isset($_REQUEST['toDateMonth']))
		$toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-31";
	else
		$toDate    = date("Y-m-d");
	/////////////////  delivery date :Stop  
   $allowance = array();
  if(isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '')
  {
    $partyId = substr_replace($_REQUEST['partyId'],"",-1);
    if(substr($_REQUEST['partyId'],-1) == 'P')
    {
    	$select = " party.partyId,party.companyName";
    	$where  = " AND deliverychallanmaster.partyId = ".$partyId."";
    	$join   = " JOIN party ON party.partyId    = deliverychallanmaster.partyId";
    }
    else
    {
    	$select = " supplier.supplierId,supplier.companyName";
    	$where  = " AND deliverychallanmaster.supplierId = ".$partyId."";
    	$join   = " JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId";
    }
  
    $selectDeliveryChallan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                                     deliverychallanmaster.srNo,deliverychallanmaster.status,user.userId,user.userName,
                                     DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                                     ".$select."
	  			  						        FROM deliverychallanmaster
	  				 						        JOIN user ON user.userId      = deliverychallanmaster.userId
	  				 						             ".$join."
	  										       WHERE deliverychallanmaster.delivaryDate >= '".$fromDate."'
                                 AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                                     ".$where."";                   
    $selectDeliveryResult = mysql_query($selectDeliveryChallan);
	  while($deliveryChallanRow = mysql_fetch_array($selectDeliveryResult))
	  {
	    $deliveryChallanArray[$deliveryChallanCount]['deliveryChallanMasterId']  = $deliveryChallanRow['deliveryChallanMasterId'];
	    $deliveryChallanArray[$deliveryChallanCount]['companyName']              = $deliveryChallanRow['companyName'];
	    $companyName                                                             = $deliveryChallanRow['companyName'];
	    $deliveryChallanArray[$deliveryChallanCount]['srNo']                     = $deliveryChallanRow['srNo'];
	    $deliveryChallanArray[$deliveryChallanCount]['delivaryDate']             = $deliveryChallanRow['delivaryDate'];
	    $deliveryChallanArray[$deliveryChallanCount]['challanType']              = $deliveryChallanRow['challanType'];
	    $challanType                                                             = $deliveryChallanRow['challanType'];
	    $deliveryChallanArray[$deliveryChallanCount]['display']                  = 0;
	    $selectdeliverychallandetail = "SELECT deliverychallandetail.deliveryChallanMasterId,deliverychallandetail.deliveryChallanDetailId,deliverychallandetail.discription,
	                                           deliverychallandetail.itemId,
                                             deliverychallandetail.subCategoryId,deliverychallandetail.quantity,remainQtyAftrFM,item.itemId,item.itemName,subcategory.subCategoryId,
                                             subcategory.subCategoryName
                                        FROM deliverychallandetail
                                        JOIN item ON item.itemId = deliverychallandetail.itemId
                                        JOIN subcategory ON subcategory.subCategoryId = deliverychallandetail.subCategoryId
                                       WHERE deliverychallandetail.deliveryChallanMasterId =  ".$deliveryChallanRow['deliveryChallanMasterId']."
                                         AND isFinalCreated = 'N'";
      $deliverychallandetailQuery = mysql_query($selectdeliverychallandetail);
      $deliveryDetailCount = 0; 
      $allowance[$deliveryChallanCount][$deliveryDetailCount]['allow'] = 0;    
      while($deliverychallandetailRow = mysql_fetch_array($deliverychallandetailQuery))
      {
        //Below logic is not completed Mehboob : Starts
      	if(isset($allowance[$deliveryChallanCount][$deliveryDetailCount-1]['allow']))
      	{
      	  $allowance[$deliveryChallanCount][$deliveryDetailCount]['allow'] = 1;
      	}
      	else
      	{
      	  $allowance[$deliveryChallanCount][$deliveryDetailCount]['allow'] = 0;
      	}
      	//Below logic is not completed Mehboob : Starts
      	$deliveryChallanArray[$deliveryChallanCount]['display'] = 1;
  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['deliveryChallanDetailId'] = $deliverychallandetailRow['deliveryChallanDetailId'];
  	    if($deliverychallandetailRow['discription'] != "")
   	    {
   	      $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['itemName'] = $deliverychallandetailRow['itemName']." # ".$deliverychallandetailRow['discription'];
   	    }
  	    else
   	    {
   	      $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['itemName'] = $deliverychallandetailRow['itemName'];
   	    }
  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['subCategoryName'] = $deliverychallandetailRow['subCategoryName'];
  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['quantity']        = $deliverychallandetailRow['quantity'];
  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['remainQtyAftrFM'] = $deliverychallandetailRow['remainQtyAftrFM'];
  	    $deliveryDetailCount++;
      }
	    $deliveryChallanCount++;
	  }
	  $selectMaxSr = "SELECT MAX(fmSrNo) + 1 AS fmSrNo
                           FROM masterdelivery ";
    $selectMaxSrRes = mysql_query($selectMaxSr);
    if($srRow = mysql_fetch_array($selectMaxSrRes))
    {
    	if($srRow['fmSrNo'] == NULL)
    	  $srRow['fmSrNo'] = 1;
    	
    	$fmSrNo = 'FM'.$srRow['fmSrNo'];
    	$srNo   = $srRow['fmSrNo'];
    }
  }
  include("./bottom.php");
  $smarty->assign("msg",$msg);
  $smarty->assign("srNo",$srNo);
  $smarty->assign("fmSrNo",$fmSrNo);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("challanType",$challanType);
  $smarty->assign("deliveryChallanArray",$deliveryChallanArray);
  $smarty->assign("allowance",$allowance);
  $smarty->assign("deliveryDetailArray",$deliveryDetailArray);
  $smarty->display("masterDeliveryAj.tpl");
}
?>